package com.the_ring.chapter04.two;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;


import com.the_ring.chapter04.R;

public class AActivity extends AppCompatActivity {

    TextView messageTv;

    ActivityResultLauncher mLauncher;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aactivity);



        messageTv = findViewById(R.id.tv_text);
        findViewById(R.id.bt_toB).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AActivity.this, BActivity.class);
                intent.putExtra("message", "来自 A 的 message");
                mLauncher.launch(intent);
            }
        });

        mLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                String message = result.getData().getStringExtra("message");
                Log.i("aaa", result.toString());
                String str = "收到 B 返回的信息：" + message;
                messageTv.setText(str);
            }
        });
    }
}